package edu.bw.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;

@Component
public class ConnectionFactory {
    @Autowired
    private ThreadLocal<Connection> threadLocal;
    @Autowired
    private DataSource dataSource;

    public Connection getConnection() {
        Connection connection = threadLocal.get();
        if (connection == null) {
            try {
                connection = dataSource.getConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
            threadLocal.set(connection);
        }
        return connection;
    }
}
